"use client";
import {
  PageContainer,
  ProForm,
  ProFormRadio,
  ProFormText,
  ProFormTextArea,
} from "@ant-design/pro-components";
import { useRequest } from "ahooks";
import { Button, Card, Col, message, Row, Space } from "antd";
import { useParams, useRouter, useSearchParams } from "next/navigation";
import { RollbackOutlined } from "@ant-design/icons";
import {
  addConfigApi,
  updateConfigApi,
  getConfigApi,
} from "@/lib/api/system/config";
import { Config } from "@/lib/api/system/config.d";

export default function Page() {
  const { type } = useParams();
  const router = useRouter();
  const params = useSearchParams();
  const configId = params.get("configId");

  const title = type === "add" ? "新增参数" : "修改参数";

  const { run: addConfig, loading: addConfigLoading } = useRequest(
    addConfigApi,
    {
      manual: true,
      onSuccess: () => {
        message.success("新增成功");
        router.back();
      },
      onError: (error: any) => {
        message.error(error.msg);
      },
    },
  );

  const { run: updateConfig, loading: updateConfigLoading } = useRequest(
    (params) => updateConfigApi({ ...params, configId: configId }),
    {
      manual: true,
      onSuccess: () => {
        message.success("修改成功");
        router.back();
      },
      onError: (error: any) => {
        message.error(error.msg);
      },
    },
  );

  const extraProps =
    type !== "add"
      ? {
          request: async () => {
            const res = await getConfigApi(configId as string);
            return res.data;
          },
        }
      : {
          defaultFormValues: {},
        };

  return (
    <PageContainer
      title={title}
      extra={
        <Button icon={<RollbackOutlined />} onClick={() => router.back()}>
          返回
        </Button>
      }
    >
      <Card>
        <ProForm
          layout="horizontal"
          submitter={{
            render: (_, dom) => {
              return (
                <Row>
                  <Col span={14} offset={4}>
                    <Space>{dom}</Space>
                  </Col>
                </Row>
              );
            },
            searchConfig: { submitText: "保存" },
          }}
          className="max-w-[800px] mx-auto my-10"
          labelCol={{ span: 4 }}
          wrapperCol={{ span: 14 }}
          loading={addConfigLoading || updateConfigLoading}
          onFinish={async (values: Config) => {
            if (type === "add") {
              await addConfig(values);
            } else {
              await updateConfig(values);
            }
          }}
          {...extraProps}
        >
          <ProFormText
            name="configName"
            label="参数名称"
            required
            rules={[{ required: true, message: "请输入参数名称" }]}
          />
          <ProFormText
            name="configKey"
            label="参数键名"
            rules={[{ required: true, message: "请输入参数键名" }]}
          />
          <ProFormText
            name="configValue"
            label="参数键值"
            rules={[{ required: true, message: "请输入参数键值" }]}
          />
          <ProFormRadio.Group
            name="configType"
            label="是否内置"
            initialValue="Y"
            options={[
              { label: "是", value: "Y" },
              { label: "否", value: "N" },
            ]}
          />
          <ProFormTextArea name="remark" label="备注" />
        </ProForm>
      </Card>
    </PageContainer>
  );
}
